package com.ufox.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ufox.entity.Result;
import com.ufox.entity.Staff;
import com.ufox.entity.StaffPassword;
import com.ufox.mapper.StaffMapper;
import com.ufox.req.PasswordREQ;
import com.ufox.service.IStaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ufox.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 员工信息表 服务实现类
 * </p>
 *
 * @author 优狐科技-www.hbufox.com
 * @since 2022-05-31
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements IStaffService {

    @Autowired
    private StaffMapper staffMapper;


    @Override
    public Result updatePassword(PasswordREQ req) {
        if (req == null || StringUtils.isEmpty(req.getPassword())) {
            return Result.error("新密码不能为空");
        }
        // 更新密码
        Staff staff = baseMapper.selectById(req.getUserId());
        staff.setPassword(MD5Util.getMD5String(req.getPassword()));
        baseMapper.updateById(staff);
        Result result = Result.ok();
        result.setMessage("修改成功");
        return result;
    }

    @Override
    public Result checkPassword(PasswordREQ req) {
        if (req == null || StringUtils.isEmpty(req.getPassword())) {
            return Result.error("原密码不能为空");
        }
        Staff staff = baseMapper.selectById(req.getUserId());
        MD5Util.getMD5String(req.getPassword());
        String password = staff.getPassword();

        Result result = Result.ok();
        if (!password.equals(MD5Util.getMD5String(req.getPassword()))) {
            result.setFlag(false);
        }else{
            result.setFlag(true);
        }
        return result;
    }
}
